﻿var mode_playing  = false;
var mode_mute = false;
var urlIndex = 0;

var fmUrls = new Array();
var fmNames = new Array();
fmUrls = ["http://38.101.195.5:8132/", "http://38.101.195.5:8032/","http://lalima.org:8500/","http://216.235.91.32:21434/","http://216.235.94.15:21668/"];
fmNames = ["Telugu One Radio - TORI","Telugu TORI - Australia","Aakasavani Radio - Telugu","Ilayaraja Telugu Songs","Telugu New Releases"];
	
	

function onclick_play() {
   changeUrl(urlIndex);
   play_btn.visible = false;
   stop_btn.visible = true;
   play();
}
function onclick_stop() {
   play_btn.visible = true;
   stop_btn.visible = false;
   stop(); 
}

function  volume_onchange() {
    _PebbleFmPlayer.settings.volume = volumeBar.value;
}
function onclick_plus() {
  volume_increase();
}
function onclick_minus() {
  volume_decrease();
}
function onclick_up() {
  //Next Fm Feed
  onclick_stop();
  if(++urlIndex>=fmUrls.length)
     urlIndex = --urlIndex;

  changeUrl(urlIndex);
  onclick_play();
}
function onclick_down() {
 //Prev Fm Feed
  onclick_stop();
  if(--urlIndex<0)
     urlIndex = ++urlIndex;
  changeUrl(urlIndex);
  onclick_play();
}

function changeUrl(i) {
  _PebbleFmPlayer.URL = fmUrls[i];
  radio_label.innerText = fmNames[i] + " ";
}

function onclick_list() {
  //addFmList();
  //fmList.visible=true;  
}


function addFmList(){

	if(fmNames.length>0){
    var i = 0;
		for (; i < fmNames.length; i++) {
			fmList.appendElement('<div x="1" y="'+(i*20)+'" width="95" height="20" ><label x="1" y="1" width="95" height="20" color="#009011" font="Tahoma" size="9" >'+fmNames[i]+'</label></div>');
		}
	  fmList.height = i*20;
	}else{
		StationList.appendElement('<label x="1" y="1" width="100" color="#009011" font="Tahoma" size="5" >No Stations</label>');
	}
  
}


//Media Player Controls
_PebbleFmPlayer = WMPlayer.object;

var volume_value = 50;

function view_onOpen() {
  volumeBar.value = volume_value;
  changeUrl(urlIndex);
	view.setInterval(displayPlayerStatus, 1000);
  view.setInterval(marqueeLabel, 200);
}

function marqueeLabel() {
  var tmp = radio_label.innerText;
  var tmpStr = new String(tmp);

  if(tmpStr.length>0){ 
    tmp = tmpStr.substring(1);
    tmp = tmp + tmpStr.substring(0,1); 
    radio_label.innerText = tmp;
  }
}

function play(){
	_PebbleFmPlayer.controls.play();
}

function stop(){
	_PebbleFmPlayer.controls.stop();
}

function unmute(){
	_PebbleFmPlayer.settings.volume = 50;
}

function mute(){
  _PebbleFmPlayer.settings.volume = 0;
}

function volume_increase() {
	if(volume_value != null) {
     if(volume_value < 100) {
	     volume_value = volume_value + 10;
	     _PebbleFmPlayer.settings.volume = volume_value;
	   }
  }
}

function volume_decrease() {
  if(volume_value != null) {
	  if(volume_value > 0) {
	     volume_value = volume_value - 10;
	        _PebbleFmPlayer.settings.volume = volume_value;
	  }
  }
}

function displayPlayerStatus() {
	if(_PebbleFmPlayer.playState == 0)
		playStatus.innerText = "Undefined";
	else if(_PebbleFmPlayer.playState == 1)
		playStatus.innerText = "Stopped";
	else if(_PebbleFmPlayer.playState == 2)
		playStatus.innerText = "Paused";
	else if(_PebbleFmPlayer.playState == 3)
		playStatus.innerText = "Playing";
	else if(_PebbleFmPlayer.playState == 4)
		playStatus.innerText = "ScanForward";
	else if(_PebbleFmPlayer.playState == 5)
		playStatus.innerText = "ScanReverse";
	else if(_PebbleFmPlayer.playState == 6)
		playStatus.innerText = "Buffering";
	else if(_PebbleFmPlayer.playState == 7)
		playStatus.innerText = "Waiting";
	else if(_PebbleFmPlayer.playState == 8)
		playStatus.innerText = "MediaEnded";
	else if(_PebbleFmPlayer.playState == 9)
		playStatus.innerText = "Transitioning";
	else if(_PebbleFmPlayer.playState == 10)
		playStatus.innerText = "Ready";
	else if(_PebbleFmPlayer.playState == 11)
		playStatus.innerText = "Reconnecting";

  if(_PebbleFmPlayer.playState== 1 || _PebbleFmPlayer.playState==2 
|| _PebbleFmPlayer.playState==8 || _PebbleFmPlayer.playState==10)
    onclick_stop();    
}